import axios from "axios";

export const request = axios.create({
  baseURL: "/api",
  timeout: 30 * 1000,
});
request.interceptors.request.use(
  (config) => {
    return config;
  },
  (err) => {},
);

request.interceptors.response.use(
  (res) => {
    // 响应的统一处理
    const status = res.data.code || 200;
    const msg = res.data.message || "未知错误";
    if (status === 401) {
      alert("没有权限");
      return Promise.reject(new Error(msg));
    } else if (status !== 200) {
      alert("错误码" + status + "  " + msg);
      return Promise.reject(new Error(msg));
    }
    return res.data;
  },
  (err) => {
    alert("响应错误");
    return Promise.reject(new Error(err));
  },
);